﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using hachiModel;
using System.IO;
using System.Text;

public partial class Admin_ProductsCategories : System.Web.UI.Page
{
    Hachi.Controllers.CategoriesController catctr = new Hachi.Controllers.CategoriesController();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSyncCate_Click(object sender, EventArgs e)
    {
        string acc = @"<div id='accordion'>";
        foreach (hachiModel.PR_Category item in catctr.GetRootCategoriesCanUse())
	    {
            PR_CategoryDetail itemdetail = catctr.getCategoryDetail(item.CategoryID, dioLang.SelectedValue);
            if (itemdetail != null)
            {
                acc += "<h3><a href='"+itemdetail.Alias+"'><i class='"+itemdetail.Icon+"'></i>"+itemdetail.CategoryName+"</a></h3>";
                acc += "<div>";
                foreach (hachiModel.PR_Category c in catctr.GetChildrenCategoriesCanUse(item.CategoryID))
                {
                    PR_CategoryDetail cdetail = catctr.getCategoryDetail(c.CategoryID, dioLang.SelectedValue);
                    if (cdetail != null)
                    {
                        acc += "<a href='" + cdetail.Alias + "'>" + cdetail.CategoryName + "</a>";
                    }
                }
                acc += "<a href='" + itemdetail.Alias + "'>" + itemdetail.CategoryName + "... </a>";
                acc += "</div>";
            }
        }
        acc += "</div>";
        StringBuilder newFile = new StringBuilder();
        string temp = "";
        string filepath = MapPath(@"../js/lang/" + dioLang.SelectedValue + "/hachicommom.js");
        string[] file = File.ReadAllLines(filepath);
        foreach (string line in file)
        {
            if (line.Contains("var hachicategorymenu"))
            {
                temp = "var hachicategorymenu = \""+ acc+"\"";
                newFile.Append(temp + "\r\n");
                continue;
            }
            newFile.Append(line + "\r\n");
        }
        File.WriteAllText(filepath, newFile.ToString());
        txtCategoryCode.Text = acc;
    }
    protected void btnBuildAlias_Click(object sender, EventArgs e)
    {
        catctr.BuildAlias();
        txtCategoryCode.Text = "Done";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Hachi.Controllers.ProductsController pctr = new Hachi.Controllers.ProductsController();
        txtCategoryCode.Text = pctr.BuildAlias();
    }
}